import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class TipRoute extends StatefulWidget {
  const TipRoute({super.key, required this.text});

  final String text;

  @override
  State<TipRoute> createState() => _TipRouteState();
}

class _TipRouteState extends State<TipRoute> {
  final TextEditingController _controller = TextEditingController();

  String inputValue = '';

  void _handleChange(String changed) {
    inputValue = changed;
  }

  @override
  void initState() {
    super.initState();
    inputValue = widget.text.endsWith('2') ? '我是返回内容2' : '我是返回内容1';
    _controller.text = inputValue;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('提示'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(18),
        child: Center(
          child: Column(
            children: [
              Container(
                margin: const EdgeInsets.only(bottom: 10),
                child: Image.asset(
                  'assets/images/logo.png',
                  width: 100,
                ),
              ),
              Container(
                margin: const EdgeInsets.only(bottom: 10),
                child: Image.network(
                  'https://book.sunxiaodou.com/assets/logo-ffd6b37f.png',
                  width: 100,
                ),
              ),
              Text(
                widget.text,
                style: TextStyle(
                  color: Colors.blue,
                  background: Paint()..color = Colors.yellow,
                  decoration: TextDecoration.underline,
                  decorationColor: Colors.purple,
                  decorationStyle: TextDecorationStyle.wavy,
                ),
              ),
              Container(
                margin: const EdgeInsets.symmetric(vertical: 5),
                child: TextField(
                  controller: _controller,
                  decoration: const InputDecoration(
                    labelText: '返回的内容',
                    hintText: '请输入返回的内容',
                  ),
                  keyboardType: TextInputType.text,
                  textInputAction: TextInputAction.done,
                  maxLength: 10,
                  maxLengthEnforcement: MaxLengthEnforcement.enforced,
                  onChanged: _handleChange,
                ),
              ),
              ElevatedButton(
                onPressed: () {
                  if (inputValue.isEmpty) {
                    ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text('请输入返回的内容')));
                    return;
                  }

                  inputValue = widget.text.endsWith('2') ? '$inputValue-2' : '$inputValue-1';
                  Navigator.of(context).pop(inputValue);
                },
                child: const Text('返回'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
